package com.drug.mingkang.controller;

import com.drug.mingkang.model.vo.*;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.HqInventoryService;
import com.drug.mingkang.utils.EarlyWarning;
import com.drug.mingkang.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 总公司库存表(HqInventory)表控制层
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:13
 */
@RestController
@RequestMapping("hqInventory")
@Api(value = "总公司库存表控制层", tags = "总公司库存表Contorller")
public class HqInventoryController {
    /**
     * 服务对象
     */
    @Resource
    private HqInventoryService hqInventoryService;
    @Resource
    private Tools tools;

    /**
     * 查询原材料所有库存
     * @param vo 原材料库存参数
     * @return 原材料库存列表
     */
    @ApiOperation(value = "查询原材料所有库存", notes = "查询原材料所有库存", httpMethod = "POST", response = Map.class)
    @PostMapping("showAll")
    public Map<String, Object> showAll(MaterialInventoryVo vo) {
        List<MaterialInventoryVo> list = hqInventoryService.selectMaterialAll(vo);
        int count = hqInventoryService.selectMaterialCount(vo);
        return tools.limit(list, count);
    }

    /**
     * 首页查询所有原材料库存明细
     * @param vo 原材料库存明细参数
     * @return 原材料库存明细列表
     */
    @ApiOperation(value = "首页查询所有原材料库存明细", notes = "首页查询所有原材料库存明细", httpMethod = "POST", response = Map.class)
    @PostMapping("materialsDetails")
    public Map<String, Object> materialsDetails(MaterialInventoryDetailVo vo) {
        List<MaterialInventoryDetailVo> list = hqInventoryService.findMaterialsDetails(vo);
        int count = hqInventoryService.findMaterialsDetailsCount(vo);
        return tools.limit(list, count);
    }

    /**
     * 首页查询所有药品库存明细
     * @param vo 药品库存明细参数
     * @param id 公司id
     * @return 药品库存明细列表
     */
    @ApiOperation(value = "首页查询所有药品库存明细", notes = "首页查询所有药品库存明细", httpMethod = "POST", response = Map.class)
    @PostMapping("drugDetails")
    public Map<String, Object> drugDetails(DrugInventoryDetailVo vo, @ApiParam(name = "companyId", value = "公司id", required = true)String id) {
        List<DrugInventoryDetailVo> list = hqInventoryService.findDrugDetails(vo, id);
        int count = hqInventoryService.findDrugDetailsCount(vo, id);
        return tools.limit(list, count);
    }

    /**
     * 首页查询所有原材料库存预警
     * @param vo 原材料库存参数
     * @return 原材料库存列表
     */
    @ApiOperation(value = "首页查询所有原材料库存预警", notes = "首页查询所有原材料库存预警", httpMethod = "POST", response = Map.class)
    @PostMapping("findMaterialsWarning")
    public Map<String, Object> findMaterialsWarning(PurchaseMaterialVo vo) {
        List<PurchaseMaterialVo> list = hqInventoryService.findMaterialsWarning(vo, EarlyWarning.ZGS_YCLKCYJSL);
        int count = hqInventoryService.findMaterialsWarningCount(vo, EarlyWarning.ZGS_YCLKCYJSL);
        Map<String, Object> limit = tools.limit(list, count);
        limit.put("redAlert",EarlyWarning.ZGS_YCLHSYJSL);
        return limit;
    }

    /**
     * 首页查询库存预警的个数
     * @param session 用户session
     * @return 库存预警数量
     */
    @ApiOperation(value = "首页查询库存预警的个数", notes = "首页查询库存预警的个数", httpMethod = "POST", response = int.class)
    @PostMapping("findCount")
    public int findCount(HttpSession session) {
        return hqInventoryService.findCount(session);
    }

    /**
     * 查询总公司公司药品库存
     * @param inventoryVo 药品库存参数
     * @return 药品库存列表
     */
    @ApiOperation(value = "查询总公司公司药品库存", notes = "查询总公司公司药品库存", httpMethod = "POST", response = Map.class)
    @PostMapping("/showDrugInventoryAll")
    public Map<String, Object> showDrugInventoryAll(InventoryVo inventoryVo) {
        return hqInventoryService.showDrugInventoryAll(inventoryVo);
    }

    /**
     * 根据库存id查看出入库明细
     * @param id 药品id
     * @param page 页
     * @param limit 条
     * @return 出入库明细列表
     */
    @ApiOperation(value = "根据库存id查看出入库明细", notes = "输入药品id", httpMethod = "POST", response = Map.class)
    @PostMapping("/findByRepertoryId")
    public Map<String, Object> findByRepertoryId(String id,int page,int limit) {
        return hqInventoryService.findByRepertoryId(id,page,limit);
    }

    /**
     * 查询总(分)公司 药品库存预警
     *
     * @param page    页
     * @param limit   条
     * @param session 当前会话
     * @return Map
     */
    @ApiOperation(value = "查询总(分)公司 药品库存预警",
            notes = "查询总(分)公司 药品库存预警", httpMethod = "POST", response = Map.class)
    @PostMapping("findAllDrugEarlyWarning")
    public Map<String, Object> findAllDrugEarlyWarning(int page, int limit, HttpSession session) {
        PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
        if ("0".equals(perUsers.getCompanyId())) {
            Map<String, Object> allDrugEarlyWarning = this.hqInventoryService.findAllDrugEarlyWarning(EarlyWarning.ZGS_YPKCYJ, "0", page, limit);
            allDrugEarlyWarning.put("redAlert",EarlyWarning.ZGS_YPHSKCYJ);
            return allDrugEarlyWarning;
        } else {
            Map<String, Object> allDrugEarlyWarning = this.hqInventoryService.findAllDrugEarlyWarning(EarlyWarning.FGS_YPKCYJ, perUsers.getCompanyId(), page, limit);
            allDrugEarlyWarning.put("redAlert",EarlyWarning.FGS_YPHSKCYJ);
            return allDrugEarlyWarning;
        }
    }

}